/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.service.battle.coreBattleLogic.singleCardType;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.hardCode.CardType;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.battle.coreBattleLogic.AbstractSingleLogic;
import com.dc.gameserver.serverCore.service.cardModule.CardBox;
import com.dc.gameserver.serverCore.service.cardModule.CardBoxManager;
import com.dc.gameserver.serverCore.service.character.GameCharacter;
import org.springframework.stereotype.Service;

/**
 * @author 石头哥哥</br>
 *         Project : gameServer1.1</br>
 *         Date: 13-11-2  11</br>
 *         Time: 下午11:02</br>
 *         Connect: 13638363871@163.com</br>
 *         packageName: Server.ServerCore.Service.fightProcess.singleCardType</br>
 *         注解：
 */
@Service
public class singleMagicLogic extends AbstractSingleLogic {
    @Override
    public void PostConstruct() {
        SINGLES[CardType.MAGIC_TYPE.getType()]=this;
    }

    /**
     * 处理不同类型牌的逻辑
     * @param p1   出牌玩家  ，p1使用任何牌有效   p1对象使用技能
     * @param p2   没有出牌的玩家
     */
    @Override
    public void SingleLogicHandle(GameCharacter p1,GameCharacter p2) {
        CardBox cardBox= CardBoxManager.getCardBoxByID(p1.getCardBoxID());
        //判定玩家相克性      p1--->p2
        //1 .相克  a-->b ; 2.不相克 a---b; 3.被克 a<---b;
        int garms= GameFormula.grams(p1.getSkillfiveType(),p2.getSelfFiveType());
        int p2Hurt=0;  //p2 造成的伤害
        int p2Shield=p2.getCurrentShield();
//        int p2Blood=p2.getCurrentBlood();
        int p2Defence=p2.getDefence();
        int p1FiveTypeSkillValue=p1.getFiveTypeSkillValue();
        if (garms== 0x1){
            p2Hurt=GameFormula.magicHurtWithGrams(p2Defence, p1FiveTypeSkillValue);
        }else if (garms== 0x2){
            p2Hurt=GameFormula.magicHurtWithUnGrams(p2Defence, p1FiveTypeSkillValue);
        } else {
            p2Hurt=GameFormula.magicHurtWithGramsed(p2Defence,p1FiveTypeSkillValue);
        }
        if (p2Hurt<=0){
            p2Hurt=0;
            p2Hurt= Config.DEFAULT_VALUE.GAME_VALUE.hurt;
        }
        //p1 法力值消耗
        int p1Magic=GameFormula.computeMagicUse(p1); // notify
        if (p1Magic!=0){
            p1.setCurrentMagic(p1.getCurrentMagic()-p1Magic);
        }

//        if (p2Shield>p2Hurt){
//           p2.setCurrentShield(p2Shield-p2Hurt);
//        } else {
//            if (p2Shield==0){
//                p2.setCurrentBlood(p2Blood-p2Hurt);
//            }else {
//                p2.setCurrentBlood(p2Blood+p2Shield-p2Hurt);
//                p2.setCurrentShield(0);
//            }
//        }

        p2.setCurrentShield((p2Shield-p2Hurt));
        cardBox.sendHurtRule(p2.getID(),p2Hurt,p1.getBattleSkillID());
        if (p2.getCurrentShield()<=0){
            p2.setCurrentShield(0);
//            p2.setCurrentBlood(0);
            //被攻击掉血
            cardBox.flushBloodAndMagic();
            cardBox.notifyPkResult(p1);
            CardBoxManager.clearPkResourceWithRoundOver(cardBox.getCardBoxID());
        } else {
            cardBox.flushBloodAndMagic();
            cardBox.resetRoundResource();
        }
    }
}
